/*
	About: Description
		This file contains controllers concerned with autogeneration of menus, navigations and sitemaps, based on pages hierarchy.
*/

/*****************************************************************************/
exports.functions={
	/*
		Function: functions.nav_calc_page_path
			
		Calculates breadcrumbs and things.
		Stores information in space.calc_pages

		Parameters:
		
		Returns:
			Stores information in space.calc_pages as is:
			* space.calc_pages.active - hash with keys of active pages, values is always '1'
			* space.calc_pages.root_id - root page id, for example id of '/vendors/' when user is on '/vendors/samsung/'
			* space.calc_pages.path - an array of pages of breadcrumbs, such as [ '/vendors/', '/vendors/samsung/' ]
			* space.calc_pages.pages - hash with keys of pages, values with info on page
	*/
	nav_calc_page_path: function(space,vars) {
		if (space.calc_pages) return;
		space.calc_pages={active: {},root_id:undefined,path:[],pages:{}};
		if (!space.pageinfo) return;
		var p=space.pageinfo.id;
		space.calc_pages.root_id=p;
		space.calc_pages.active[p]=1;
		space.calc_pages.pages[p]=space.pageinfo;
		p=space.pageinfo.parent_id;
		while (p) {
			space.calc_pages.root_id=p;
			space.calc_pages.active[p]=1;
			var page=space.mfact.get("page",p);
//			var page=space.site.sql.execute_and_fetch_one("nav/get_page",{page_id:p});
			space.calc_pages.pages[page.id]=page;
			space.calc_pages.path.unshift(page);
			p=page.parent_id;
		}
	},
	/*
		Function: functions.nav_set_active_pages

		Iterates an array of pages and sets .active property for onces which ids present in space.calc_pages.active[]

		Parameters:
			arr - array of pages

		Returns:
			none
	*/
	nav_set_active_pages: function (space,arr) {
		
		arr.forEach(function(x) {
			x.active=(space.calc_pages.active[x.id])?1:0;
			//if (!x.active) x.active=(space.calc_pages.active[x.page_id])?1:0;
			x.fullactive=(x.id==space.pageinfo.id)?1:0;
		});
	},
	nav_list_pages: function(space,hier,parent_id,level) {
		if (!level) level=1;
		var filter={order_by:"main.ordering",show_in_nav:1};
		if (parent_id) filter.parent_id=parent_id; else filter.parent_id={type:"null"};
		if (space.uid) filter.hide_reg=0; else filter.hide_anon=0;
		if (space.role_ids) {
			var a=[];
			for (var id in space.role_ids) a.push(id);
//			filter.role_id={type:"null"};
			filter.role_id={type: "in",value: a,or_null:1};
		} else {
			filter.role_id={type:"null"};
		}
		var r=space.mfact.list("page",filter);
		var r2=[];
		r.forEach(function (p) {
			r2.push(p);
			if (p.alias=="/index/") p.alias="/";
			p.level=level;
			if (hier) space.functions.nav_list_pages(space,1,p.id,level+1).forEach(function(p2) {r2.push(p2)});
		});
		return r2;
	}
};

/*****************************************************************************/
exports.controllers={
	/*
		Function: controllers.nav_root

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/root"
			options.parent_id - optional, root of navigation tree (id from Tpages), defaults to null
		
		Returns:
			Valid HTML
	*/
	nav_root: function(space,options) {
		if (!options) options={template: "nav/root"};
		if (typeof(options)=="string") options={template: options};
		space.functions.nav_calc_page_path(space);
		if (!space.nav_root) {
			space.nav_root=space.functions.nav_list_pages(space,0,options.parent_id);
			if (options.abstract) {
				space.nav_root.forEach(function(p) {
					var tmp=space.site.sql.execute_and_fetch_one("nav/abstract",{page_id:p.id});
					if (tmp) p.abstract=tmp.abstract;
				});
			}
//			space.nav_root=space.site.sql.execute_and_fetch("nav/root",space);
			space.functions.nav_set_active_pages(space,space.nav_root);
		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_hier

		Makes a full tree navigation

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/hier"

		Returns:
			Valid HTML

	*/
	nav_hier: function(space,options) {
		if (!options) options={template: "nav/hier"};
		if (typeof(options)=="string") options={template: options};
		space.functions.nav_calc_page_path(space);
		if (!space.nav_hier) {
			space.nav_hier=space.site.sql.execute_and_fetch_h("nav/hier",{});
			space.functions.nav_set_active_pages(space,space.nav_hier);
		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_root_dropdown
	
		Makes a root navigation with drop-down

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/root_dropdown"

		Returns:
			Valid HTML	
	*/
	nav_root_dropdown: function(space,options) {
		if (!options) options={template: "nav/root_dropdown"};
		if (typeof(options)=="string") options={template: options};
		space.functions.nav_calc_page_path(space);

		function recurse_fill(items,level) {
			space.functions.nav_set_active_pages(space,items);
			items.forEach(function(p) {
				p.level=level;
				var si=space.functions.nav_list_pages(space,0,p.id);
				if (!(si && si.length)) return;
				p.subitems=si;
				if (level<5) recurse_fill(p.subitems,level+1);
			});
		}
		if (!space.nav_root_dropdown) {
			space.nav_root_dropdown=space.functions.nav_list_pages(space,0,options.parent_id);
			recurse_fill(space.nav_root_dropdown,1);
		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_no_root

		Makes a sub-tree navigation of current section, without root.
		Useful with nav_root().

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/no_root"
			options.max_sublevels - optional, maximum number of sublevels to display, defaults to null
			options.root_id - optional, override root of navigation tree, defaults to space.calc_pages.root_id

		Returns:
			Valid HTML
	*/
	nav_no_root: function(space,options) {
		if (typeof(options)=="string") options={template: options};
		if (!options) options={};
		if (!options.template) options.template="nav/no_root";
		space.functions.nav_calc_page_path(space);
		var root_id=options.root_id||space.calc_pages.root_id;
		if (!root_id) return "";
//		if (!space.nav_no_root) {
			space.nav_no_root=space.site.sql.execute_and_fetch_h("nav/hier",{parent_id:root_id});
			if (options.max_sublevels) {
				var tmp=space.nav_no_root;
				space.nav_no_root=[];
				tmp.forEach(function(x) {if (x.level<options.max_sublevels) space.nav_no_root.push(x);});
			}
			space.functions.nav_set_active_pages(space,space.nav_no_root);
			if (options.abstract) {
				space.nav_no_root.forEach(function(p) {
					var tmp=space.site.sql.execute_and_fetch_one("nav/abstract",{page_id:p.id});
					if (tmp) p.abstract=tmp.abstract;
				});
			}
//		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_subnav

		Makes a sub-tree navigation starting from children of current page.
		Can be used to make a list of articles.

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/subnav"
			options.parent_id - optional, id from Tpages, defaults to space.pageinfo.id
		Returns:
			Valid HTML
	*/
	nav_subnav: function(space,options) {
		if (!options) options={};
		if (!options.template) options.template="nav/subnav";
		space.functions.nav_calc_page_path(space);

		var pbs={};
		if (!options.pageblocks) pbs.navtext="abstract"; else options.pageblocks.split(/,/).forEach(function(k){pbs[k]=k;});
//		if (!space.nav_subnav) {
			space.nav_subnav=space.functions.nav_list_pages(space,1,options.parent_id||space.pageinfo.id);
//			space.nav_subnav=space.site.sql.execute_and_fetch_h("nav/hier",{parent_id:space.pageinfo.id});
			if (options.max_sublevels) {
				var tmp=space.nav_subnav;
				space.nav_subnav=[];
				tmp.forEach(function(x) {if (x.level<options.max_sublevels) space.nav_subnav.push(x);});
			}
			space.nav_subnav.forEach(function(p) {
				for (var k in pbs) {
					var t=space.mfact.get("page_block",{page_id:p.id,place_id:{type:"in", value: "select id from Ttemplate_places where code='"+k+"'",asis:1} });
					if (t) p[pbs[k]]=t.body;
//					p[pbs[k]]=t.body;
				}
			});
			space.functions.nav_set_active_pages(space,space.nav_subnav);
//		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_sitemap2

		Makes a sitemap, BUT split by root sections.

		Parameters:
			options - optional, an object of:
			options.root_id - optional, id from Tpages, defaults to null
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/sitemap2"

		Returns:
			Valid HTML
	*/
	nav_sitemap2: function(space,options) {
		if (!options) options={};
		if (!options.template) options.template="nav/sitemap2";
//		if (typeof(options)=="string") options={template: options};
		space.functions.nav_calc_page_path(space);
//		if (!space.nav_sitemap2) {
			space.nav_sitemap2=space.site.sql.execute_and_fetch("nav/hier",{uid:space.uid,parent_id:options.root_id});
			space.functions.nav_set_active_pages(space,space.nav_sitemap2);
			space.nav_sitemap2.forEach(function(x) {
				x.subnav=space.site.sql.execute_and_fetch_h("nav/hier",{parent_id:x.id});
				space.functions.nav_set_active_pages(space,x.subnav);
			});
//		}
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.nav_path

		Renders breadcrumbs.
		For "/" (root page) returns an empty result.

		Parameters:
			options - optional, an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "nav/path"

		Returns:
			Valid HTML
	*/
	nav_path: function(space,options) {
		if (space.pageinfo.alias=="/index/" || space.pageinfo.alias=="/") return "";
		if (!options) options={};
		if (!options.template) options.template="nav/path";
		space.functions.nav_calc_page_path(space);
		space.nav_path=space.calc_pages.path;
		if (options.show_index) {
			space.nav_path=space.calc_pages.path.slice(0);
			space.nav_path.unshift(space.site.sql.execute_and_fetch_one("nav/get_page_by_alias",{alias: "/index/"}));
			space.nav_path[0].show_in_nav=1;
		};
		return space.views.process("widgets/"+options.template,space);
	},

	nav_calc_page_path: function(space) {
		space.functions.nav_calc_page_path(space);
		return "";
	}
};

/*****************************************************************************/
